﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;

public partial class admin_CateProd_Details : System.Web.UI.Page
{
  ProdCateBLL data = new ProdCateBLL();
  private static int maxLevel;
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["url_back_admin"] = Request.RawUrl;

        maxLevel = GetLevelConfig();
        LoadTitle();
        LoadProdCate();
        LoadMode();
        if (Request.QueryString["Id"] != null)
          Filldata();
      }
    }
    private void LoadMode()
    {
      dropMode.Items.Clear();
      XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
      var xPosition = xElement.Descendants("ProductMode").Descendants("Mode");
      foreach (XElement item in xPosition)
      {
        dropMode.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
      }
      dropMode.Items.Insert(0,new ListItem("- Chọn nhóm sản phẩm -", "-1"));
    }
    private void Filldata()
    {
      dropMode.Enabled = false;
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      ProdCate prodCate = data.GetById(id);
      txtName.Text = prodCate.Name;
      txtName_En.Text = prodCate.Name_En;
      dropParent.SelectedValue = prodCate.Parent.ToString();
      dropMode.SelectedValue = prodCate.Mode.ToString();
      if (prodCate.isEnable) radioEnable.Checked = true;
      else radioVisible.Checked = true;
    }
    private void ShowChild(int parentId, string space)
    {
      List<ProdCate> listDataSource = data.GetByParent(parentId);

      foreach (ProdCate item in listDataSource)
      {
        ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
        dropParent.Items.Add(obj);

        ShowChild(item.Id, space + "- - - ");
      }
    }
    private void LoadProdCate()
    {
      dropParent.Items.Clear();
      ShowChild(0, " - - ");
      ListItem itemRoot = new ListItem("Danh mục gốc", "0");
      dropParent.Items.Insert(0, itemRoot);
    }
    private void LoadTitle()
    {
      if (Request.QueryString["Id"] != null)
      {
        h3Title.InnerText = "Chỉnh sửa danh mục";        
      }
    }
    private bool isValidate()
    {
      if (txtName.Text == "")
      {
        Myfunction.ThongBao(lbThongBao, "Tên danh mục không được để trống !");
        return false;
      }
      if (dropMode.SelectedIndex <= 0)
      {
        Myfunction.ThongBao(lbThongBao, "Chọn nhóm sản phẩm");
        return false;
      }
      return true;
    }
    private void ResetForm()
    {
      txtName.Text = "";
      txtName_En.Text = "";
      radioEnable.Checked = true;
    }
    private int GetLevelConfig()
    {
      XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.XML");
      var xLevel = xElement.Descendants("MaxLevel").Descendants("CategoryProduct");
      XElement level = xLevel.First();
      return Convert.ToInt32(level.Value);
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
      if ((int)Session["Role"] != 1)
      {
        Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
        return;
      }
      try
      {
        lbThongBao.Text = "";
        if (isValidate())
        {
          if (Request.QueryString["Id"] == null)
          {
            Add();
            ResetForm();
            LoadProdCate();
          }
          else
          {
            Update();           
          }         
        }
      }
      catch (Exception ex)
      {
        Myfunction.ThongBao(lbThongBao, ex.Message);
      }
    }
    private void Update()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      int id = Convert.ToInt32(Request.QueryString["Id"]);
      ProdCate prodCate = new ProdCate();
      ProdCate ProdCateCurrent = data.GetById(id);

      prodCate.Id = id;
      prodCate.Name = txtName.Text;
      prodCate.Name_En = txtName_En.Text;
      prodCate.Sort = data.GetMaxSort(idParent);
      prodCate.CreatedDate = ProdCateCurrent.CreatedDate;
      prodCate.isEnable = (radioEnable.Checked ? true : false);
      prodCate.Parent = idParent;
      prodCate.isDelete = ProdCateCurrent.isDelete;
      prodCate.Mode = Convert.ToInt32(dropMode.SelectedValue.ToString());
 
      if (idParent == 0) prodCate.Level = 1;
      else
      {
        prodCate.Level = (byte)(data.GetById(idParent).Level + 1);
        if (prodCate.Level > maxLevel)
        {
          Myfunction.ThongBao(lbThongBao,"Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }

      prodCate.Image = ProdCateCurrent.Image;
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
          if (prodCate.Image == null || prodCate.Image == "")
            prodCate.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/CateProd");
          else
            prodCate.Image = Myfunction.UpdateFile(fileUpload.PostedFile, "Images/CateProd", prodCate.Image);
        }
      }
     
      data.Update(prodCate);

      Myfunction.ThongBao(lbThongBao,"Danh mục đã được cập nhật");
    }
    private void Add()
    {
      int idParent = Convert.ToInt32(dropParent.SelectedValue);
      ProdCate prodCate = new ProdCate();

      prodCate.Name = txtName.Text;
      prodCate.Name_En = txtName_En.Text;
      prodCate.Sort = data.GetMaxSort(idParent);
      prodCate.CreatedDate = DateTime.Now;
      prodCate.isEnable = (radioEnable.Checked ? true : false);
      prodCate.Parent = idParent;
      prodCate.isDelete = true;
      prodCate.Mode = Convert.ToInt32(dropMode.SelectedValue.ToString());

      if (idParent == 0) prodCate.Level = 1;
      else
      {
        prodCate.Level = (byte)(data.GetById(idParent).Level + 1);
        if (prodCate.Level > maxLevel)
        {
         Myfunction.ThongBao(lbThongBao,"Danh mục chỉ được tối đa " + maxLevel + " cấp");
          return;
        }
      }
      if (radioDeAnh.Checked)
      {
        if (fileUpload.HasFile)
        {
          prodCate.Image = Myfunction.CopyFile(fileUpload.PostedFile, "Images/CateProd");
        }
      }
      data.Add(prodCate);
      Myfunction.ThongBao(lbThongBao, "Danh mục đã được cập nhật");
    }
    protected void radioBoAnh_CheckedChanged(object sender, EventArgs e)
    {
      if (radioBoAnh.Checked)
      {
        trFileUpload.Visible = false;
      }
      else
        trFileUpload.Visible = true;
    }
    protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
    {
      try
      {
        if (dropParent.SelectedIndex > 0)
        {
          dropMode.Enabled = false;
          ProdCateBLL data = new ProdCateBLL();
          var cate = data.GetById(Convert.ToInt32(dropParent.SelectedValue));

          dropMode.SelectedValue = cate.Mode.ToString();
        }
        else 
        {
          dropMode.Enabled = true;
          dropMode.SelectedIndex = 0;
        }
      }
      catch { }
    }
}